package com.lrh.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lrh.reggie.common.CustomException;
import com.lrh.reggie.common.R;
import com.lrh.reggie.dto.SetmealDto;
import com.lrh.reggie.entity.*;
import com.lrh.reggie.mapper.SetmealMapper;
import com.lrh.reggie.service.CategoryService;
import com.lrh.reggie.service.SetmealDishService;
import com.lrh.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐,需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal_dish,执行insert 操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作  saveBatch批量保存
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 套餐信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> getAllPage(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>();
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Setmeal::getName, name);

        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");

        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);

            Long categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);

            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);

            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return setmealDtoPage;
    }

    /**
     * 修改套餐,需要修改套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithSetmealDish(SetmealDto setmealDto) {
        //修改套餐的基本信息，操作setmeal, 执行修改 操作
        this.updateById(setmealDto);

        //先将套餐和菜品的关联信息删除 setmeal_dish
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        // 再操作setmeal_dish，执行insert操作  saveBatch批量保存
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 套餐管理的修改套餐的数据回显
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithSetmealDish(Long id) {

        //查询套餐的基本信息，从setmeal表查
        Setmeal setmeal = setmealService.getById(id);

        SetmealDto setmealDto = new SetmealDto();

        //将setmeal的数据拷贝给setmealDto
        //因为setmealDto 继承了setmeal 拥有setmeal所有的属性，且有private List<SetmealDish> setmealDishes;
        BeanUtils.copyProperties(setmeal, setmealDto);

        //条件构造器对象  用于构造查询条件  在setmeal_dish表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);

        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    /**
     * 删除套餐和该套餐菜品关系的数据
     *
     * @param id
     */
    @Override
    @Transactional
    public void removeWithSetmealDish(String id) {
        String[] ids = id.split(",");

        for (int i = 0; i < ids.length; i++) {

            //查询套餐状态，确定是否可用删除
            LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Setmeal::getId,ids[i]);
            lambdaQueryWrapper.eq(Setmeal::getStatus,1);

            int count = this.count(lambdaQueryWrapper);
            if(count > 0){
                //如果不能删除，抛出一个业务异常
                throw new CustomException("套餐正在售卖中，不能删除");
            }

            //如果可以删除，删除setmeal表的套餐数据
            this.removeById(ids[i]);

            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, ids[i]);

            //删除setmeal_dish表的 对应的套餐菜品关系
            setmealDishService.remove(queryWrapper);
        }

    }

    /**
     * 修改套餐售卖状态  停售 启售 功能
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateStatus(int status, String id) {

        String[] ids = id.split(",");

        for (int i = 0; i < ids.length; i++) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);

            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getId, ids[i]);

            setmealService.update(setmeal, queryWrapper);
        }
    }


}
